package com.liu.security.handler;

import com.liu.security.enums.HttpStateEnum;
import com.liu.security.exception.CustomException;
import com.liu.security.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录失败触发
 */
@Component
@Slf4j
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        log.info("用户登录失败触发  ... {}",  exception.getMessage());
        ResponseUtils.endResponse(response,new CustomException(exception.getMessage(), HttpStateEnum.ERROR.getCode()));
    }
}
